package binarytree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/23 15:08
 * https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/solution/
 */
public class FindMode {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    static TreeNode pre;
    static int time, max;
    /**
     * 递归法
     * @param root
     * @return
     */
    public static int[] findMode(TreeNode root) {
        //前驱结点
        pre = null;
        //次数
        time = 0;
        //最大次数
        max =0;
        Stack<Integer> temp = new Stack<>();
        traversal(root, temp);
        int[] result = new int[temp.size()];
        for (int i = 0; i < temp.size(); i++) {
            result[i] = temp.get(i);
        }
        return result;
    }

    public static void traversal(TreeNode root, Stack<Integer> result) {
        if (root == null) {
            return;
        }
        traversal(root.left, result);
        if (pre == null) {
            time = 1;
        }else if (pre.val == root.val) {
            time++;
        } else {
            //前后两者不同，重新计数
            time = 1;
        }
        if (time == max) {
            result.push(root.val);
        }
        if (time > max) {
            max = time;
            result.clear();
            result.push(root.val);
        }
        pre = root;
        traversal(root.right, result);
    }

    /**
     * 迭代法
     * @param root
     * @return
     */
    public static int[] findMode1(TreeNode root) {
        TreeNode pre = null;
        int time = 0;
        int max = 0;
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if (pre == null) {
                time = 1;
            } else if (pre.val == root.val) {
                time++;
            }else{
                time = 1;
            }
            pre = root;
            if (time == max) {
                result.add(root.val);
            }
            if (time > max) {
                max = time;
                result.clear();
                result.add(root.val);
            }
            root = root.right;
        }
        int[] a = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            a[i] = result.get(i);
        }
        return a;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        root.right.right = new TreeNode(2);
        findMode(root);
    }
}
